﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using yncTech.Common;
using yncTech.BusinessObjects;
using System.Globalization;
using System.Text;
using System.Data.OleDb;
using System.Collections;
using EntitySpaces.Interfaces;

public partial class Admin_Sys_Dic_Dic_ThirdUnit_A : AdminBasePage
{
    public decimal? tunitID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[BFThirdUnitMetadata.ColumnNames.ThirdUnitID] != null)
        {
            tunitID = decimal.Parse(Request.QueryString[BFThirdUnitMetadata.ColumnNames.ThirdUnitID]);
        }
        if (!IsPostBack)
        {
            switch (ActionType)
            {
                case OperationType.Add:
                    btnSave.Text = "新增";
                    radActive.Checked = true;
                    break;
                case OperationType.Update:
                    if (tunitID.HasValue)
                    {
                        BFThirdUnit tunit = new BFThirdUnit();
                        if (tunit.LoadByPrimaryKey(tunitID.Value))
                        {
                            txtName.Text = tunit.Name;
                            txtMail.Text = tunit.Email;
                            txtAddress.Text = tunit.Address;
                            if (tunit.Status.HasValue && tunit.Status.Value == (int)ActiveStatus.Active)
                            {
                                radActive.Checked = true;
                            }
                            else
                            {
                                radInActive.Checked = true;
                            }
                            drpThirdType.SelectedValue = tunit.ThirdType;
                            drpApprover.SelectedValue = tunit.ManageUserId.ToString();
                            txtCartName.Text = tunit.CartName;

                            //if (tunit.IsThird.HasValue && tunit.IsThird.Value)
                            //{
                            //    radYes.Checked = true;
                            //}
                            //else
                            //{
                            //    radNo.Checked = true;
                            //}
                        }
                        btnSave.Text = "修改";
                    }
                    break;
                case OperationType.Delete:
                    btnSave_Click(null, null);
                    break;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            BFThirdUnit tunit = new BFThirdUnit();
            if (ActionType == OperationType.Add || ActionType == OperationType.Update)
            {
                tunit.Query.Where(tunit.Query.Name == txtName.Text.Trim());                          
                if (ActionType == OperationType.Update)
                {
                    tunit.Query.Where(tunit.Query.ThirdUnitID != tunitID);
                }

                tunit.Query.Load();
                if (tunit.es.HasData)
                {
                    lbMessage.Text = "机构名称已存在。";
                    return;
                }
            }
            switch (ActionType)
            {
                case OperationType.Add:
                    tunit.Query.Where(tunit.Query.Name == txtName.Text.Trim());
                    tunit.Query.Load();
                    if (tunit.es.HasData)
                    {
                        lbMessage.Text = "机构名称已存在。";
                        return;
                    }
                    tunit.Name = txtName.Text.Trim();
                    tunit.Email = txtMail.Text.Trim();
                    tunit.CartName = txtCartName.Text.Trim();
                    tunit.Address = txtAddress.Text.Trim();
                    tunit.Status = radActive.Checked ? 1 : 0;
                    tunit.LastedUpdatedBy = tunit.CreatedBy = LoginUserInfo.CurrentUser.LoginName;
                    tunit.LastedUpdatedDate = tunit.CreatedDate = DateTime.Now;
                    tunit.ThirdType = drpThirdType.SelectedValue;
                    tunit.ManageUserId = Decimal.Parse(drpApprover.SelectedValue);
                    tunit.Save();
                    break;
                case OperationType.Update:
                    tunit.LoadByPrimaryKey(tunitID.Value);
                    tunit.Name = txtName.Text.Trim();
                    tunit.Email = txtMail.Text.Trim();
                    tunit.CartName = txtCartName.Text.Trim();
                    tunit.Address = txtAddress.Text.Trim();
                    tunit.Status = radActive.Checked ? 1 : 0;
                    tunit.LastedUpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    tunit.LastedUpdatedDate = DateTime.Now;
                    tunit.ThirdType = drpThirdType.SelectedValue;
                    tunit.ManageUserId = Decimal.Parse(drpApprover.SelectedValue);
                    tunit.Save();
                    break;
                case OperationType.Delete:
                    try
                    {
                        tunit.LoadByPrimaryKey(tunitID.Value);
                        using (esTransactionScope scope = new esTransactionScope())
                        {
                            BFThirdUnit bfthirdunit = new BFThirdUnit();
                            BFThirdUnitCabinCollection bfuccollection = new BFThirdUnitCabinCollection();
                            BFThirdUnitNumberCollection buncollection = new BFThirdUnitNumberCollection();
                            FinBoxCollection finboxcollection = new FinBoxCollection();
                            FinReceiptCollection finreceiptcollection = new FinReceiptCollection();
                            ArrayList arg1 = new ArrayList();                       
                            ArrayList argBox = new ArrayList();
                            //ArrayList argBox2 = new ArrayList();
                            ArrayList argReceipt = new ArrayList();
                            //ArrayList argReceipt2 = new ArrayList();
                            bfthirdunit.Query.Where(bfthirdunit.Query.ThirdUnitID == tunitID);
                            bfthirdunit.Query.Load();
                            argBox.Add(finboxcollection.Query.BoxLocation == tunitID);
                            argReceipt.Add(finreceiptcollection.Query.StorageLocation == tunitID);
                            bfuccollection.Query.Where(bfuccollection.Query.ThirdUnitID == tunitID);
                            bfuccollection.Query.Load();
                            foreach(BFThirdUnitCabin tempCabin in bfuccollection)
                            {
                                argBox.Add(finboxcollection.Query.ThirdUnitCabinId==tempCabin.ThirdUnitCabinID.Value);
                                argReceipt.Add(finreceiptcollection.Query.ThirdUnitCabinId == tempCabin.ThirdUnitCabinID.Value);
                                arg1.Add(buncollection.Query.ThirdUnitCabinID == tempCabin.ThirdUnitCabinID.Value);                               
                            }
                             if (arg1.Count>0)
                             {
                                 buncollection.Query.Where(buncollection.Query.Or(arg1.ToArray()));
                                 buncollection.Query.Load();
                                 foreach (BFThirdUnitNumber tempNumber in buncollection)
                                 {
                                    argBox.Add(finboxcollection.Query.ThirdUnitNumberId == tempNumber.ThirdUnitNumberID.Value);
                                    argReceipt.Add(finreceiptcollection.Query.ThirdUnitNumberId == tempNumber.ThirdUnitNumberID.Value);                                
                                 }
                             }
                            if (argBox.Count > 0)
                            {
                                finboxcollection.Query.Where(finboxcollection.Query.Or(argBox.ToArray()));
                                finboxcollection.Query.Load();
                            }
                            if (argReceipt.Count > 0) {
                                finreceiptcollection.Query.Where(finreceiptcollection.Query.Or(argReceipt.ToArray()));
                                finreceiptcollection.Query.Load();
                            }
                            if (finboxcollection.HasData || finreceiptcollection.HasData)
                            {
                                WebHelper.GotoParent("无法删除已经使用的凭证存放机构!");
                                break;
                            }
                            else {
                                bfuccollection.MarkAllAsDeleted();
                                bfuccollection.Save();
                                buncollection.MarkAllAsDeleted();
                                buncollection.Save();
                                bfthirdunit.MarkAsDeleted();
                                bfthirdunit.Save();
                            }                                                                                                  
                            //WebHelper.GotoParent();
                            scope.Complete();
                        }

                    }
                    catch (Exception ex)
                    {
                        WebHelper.GotoParent("无法删除已经使用的凭证存放机构!");
                    }
                    break;
            }
            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }   
    }

}